package eo.main.gui.components.tree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

import javax.swing.tree.TreeNode;

import org.apache.commons.collections.iterators.IteratorEnumeration;

import eo.descriptor.entity.EntityDescriptor;

public class EntityTreeNodeAdaptor implements TreeNode {

    private final EntityDescriptor entity;

    private final List<TreeNode> children;

    private TreeNode parent;

    public EntityTreeNodeAdaptor(EntityDescriptor entity, TreeNode parent) {
        super();
        this.entity = entity;
        children = new ArrayList<TreeNode>();
        this.parent = parent;
        Collection<? extends EntityDescriptor> subclasses = this.entity
                .getSubclasses();
        for (EntityDescriptor entityDescriptor : subclasses) {
            children.add(new EntityTreeNodeAdaptor(entityDescriptor, this));
        }
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }
    
    public Enumeration<?> children() {
        return new IteratorEnumeration(children.iterator());
    }

    public boolean getAllowsChildren() {
        return true;
    }

    public TreeNode getChildAt(int childIndex) {
        return children.get(childIndex);
    }

    public int getChildCount() {
        return children.size();
    }

    public int getIndex(TreeNode node) {
        return children.indexOf(node);
    }

    public TreeNode getParent() {
        return this.parent;
    }

    public boolean isLeaf() {
        return this.children.isEmpty();
    }

}
